package com.usian.common.aliyun;

import com.alibaba.fastjson.JSON;
import com.usian.common.fastdfs.FastDFSClient;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Component
public class ScanTest {

    @Autowired
    private FastDFSClient fastDFSClient;

    /**
     * 阿里云审核文本
     *
     * @param content
     * @return
     */
    public  Map<String,Object> scanText(String content){
        Map<String,Object> map =new HashMap<>();

        String host = "https://title.market.alicloudapi.com";
        String path = "/scanContent/text";
        String method = "POST";
        String appcode = "076a18b0ffc44c7385a9ce044e01ef2f";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("content", content);

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //获取response的body
            String result = EntityUtils.toString(response.getEntity());
            map = JSON.parseObject(result, Map.class);
            System.out.println("文本审核结果map："+ map);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    /**
     * 阿里云审核图片
     *
     * @param bytes
     * @return
     */
    public Map<String,Object> scanImage(byte[] bytes){
        Map<String,Object> map =new HashMap<>();
        String host = "https://title.market.alicloudapi.com";
        String path = "/scanContent/img";
        String method = "POST";
        String appcode = "076a18b0ffc44c7385a9ce044e01ef2f";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */

            /**
             * 1:url fastdfs 下载本地 指定位置 file
             * 2：file 转 base64编码
             * 3：base64对接
             */

            String filePath ="D:\\shenhe.jpg";

            //远程图片转成本地文件
            File file = ImageUtil.byte2file(bytes, filePath);

            //本地文件生成base64
            String base64Str = ImageUtil.convertImageToBase64Str(filePath);

            //bodys.put("img_url", "http://192.168.58.138/group1/M00/00/01/wKg6imTuqCuAAF3BAAASp690Yak599.jpg");
            bodys.put("img_base64", base64Str);

            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //获取response的body
            String result= EntityUtils.toString(response.getEntity());
            //System.out.println(result);
            map = JSON.parseObject(result, Map.class);
            System.out.println("图片审核结果map："+ map);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }
}
